package installer.base;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.junit.Test;

public class FilterAbroad {
	
	public void filter() {
		try {
			URL url = this.getClass().getClassLoader()
					.getResource("installer/base/temp.txt");
			System.out.println(url.getPath());
			//FileWriter writer = new FileWriter(url.getPath());
			BufferedWriter bw = new BufferedWriter(
					new FileWriter(url.getPath()));
			bw.write("d");
			bw.append("dd");
			bw.newLine();
			bw.append("dd");
			bw.flush();
			bw.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@SuppressWarnings("unchecked")
	@Test
	public void nothing() {
		SAXBuilder builder = new SAXBuilder();

		Document doc = null;

		try {
			URL fileurl = this.getClass().getClassLoader()
					.getResource("installer/base/temp.txt");
			System.out.println(fileurl.getPath());
//			FileWriter writer = new FileWriter(fileurl.getPath());
			// FileReader read = new FileReader(url.getPath());
			BufferedWriter bw = new BufferedWriter(
					new FileWriter(fileurl.getPath()));

			URL url = this.getClass().getClassLoader()
					.getResource("installer/base/temp.xml");
			System.out.println(url.getPath());
			FileReader read = new FileReader(url.getPath());
			BufferedReader br = new BufferedReader(read);
			String row = null;
			StringBuffer xml = new StringBuffer();
			while ((row = br.readLine()) != null) {
				xml.append(row);
				System.out.println(row);
			}
			doc = builder.build(url);
			Element root = doc.getRootElement();
			List ls = root.getChildren();// ע��˴�ȡ������root�ڵ������һ���Element����
			int nationnum = 1;
			for (Iterator iter = ls.iterator(); iter.hasNext();) {
				Element nation = (Element) iter.next();
				nationnum++;

				String nationid = getNationid(nationnum);
				String nationrecord = nationid + ","
						+ nation.getAttributeValue("Name") + ",nation";
				System.out.println(nationrecord);
				bw.append(nationrecord);
				bw.newLine();

				List<Element> provinces = nation.getChildren();
				int provincenum = 1;

				for (Element province : provinces) {

					String provincename = null;
					if (province.getAttribute("Name") != null) {
						provincename = province.getAttribute("Name").getValue();
					} else {
						provincename = " ";
					}
					String provinceid = getProvinceid(provincenum, nationid);
					String provincerecord = provinceid + "," + provincename
							+ ",province";
					System.out.println(provincerecord);
					bw.append(provincerecord);
					bw.newLine();

					List<Element> citys = province.getChildren();
					int citynum = 1;
					for (Element city : citys) {
						String cityname = city.getAttributeValue("Name");
						String cityrecord = getCityid(citynum, provinceid)
								+ "," + cityname + ",city";
						System.out.println(cityrecord);
						bw.append(cityrecord);
						bw.newLine();
						citynum++;
					}
					provincenum++;
				}
			}
			bw.close();
			br.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (JDOMException ex) {
			ex.printStackTrace();
		}
	}

	public String getNationid(int nationidnum) {
		String nationid = "00";
		nationid += nationidnum;
		return nationid.substring(nationid.length() - 3, nationid.length())
				+ "000000";
	}

	public String getProvinceid(int provincenum, String nationid) {
		String provinceid = "00";
		provinceid += provincenum;
		provinceid = provinceid.substring(provinceid.length() - 2,
				provinceid.length());
		provinceid = nationid.substring(0, 3) + provinceid + "0000";
		return provinceid;
	}

	public String getCityid(int citynum, String provinceid) {
		String cityid = "00";
		cityid += citynum;
		cityid = cityid.substring(cityid.length() - 2, cityid.length());
		cityid = provinceid.substring(0, 5) + cityid + "00";
		return cityid;
	}
}
